import QtQuick
import QtQuick.Controls
import com.example.remoteDataBase 1.0

Window {
    id:welcomeWindow
    x:Screen.width/2-welcomeWindow.width/2
    y:Screen.height/2-welcomeWindow.height/2
    width: 300
    height: 350
    visible: false
    title: "Welcome"

    RemoteDataBase{
        id:remoteDB1
        onUserAdded:function(success,message) {
            if (success) {
                userId= message
                localConfig.writeLocalConfig("username",userName)
                localConfig.writeLocalConfig("ID",userId)
                systemTray.setUserPhoto(root.photo)
                noticeTextOK.text="您已经注册成功！"
                noticeOK.open()
            } else {
                noticeText.text="抱歉，注册失败！"
                notice.open()
            }
        }
        onUserReceived:function(message,users) {
            if (users && users.length > 0) { // 检查 users 是否有效
                var tuser = users[0]// tuser 是一个 JavaScript 对象
                root.userId=tuser.id
                root.userName=tuser.name
                root.experience=parseInt(tuser.experience)
                root.level=parseInt(tuser.level)
                root.immortalClass=parseInt(tuser.immortalclass)
                root.photo=parseInt(tuser.photo)
                systemTray.setUserPhoto(root.photo)
                localConfig.writeLocalConfig("ID",root.userId)
                localConfig.writeLocalConfig("username",root.userName)
                localConfig.writeLocalConfig("password",tuser.password)
                noticeTextOK.text="您已经登录成功！"
                noticeOK.open()
            } else {
                noticeText.text=message
                notice.open()
            }
        }
    }
    Rectangle{
        anchors.fill: parent
        Text {
            id:textWelcome
            y:20
            text: "Welcome to Experience Life!"
            anchors.horizontalCenter: parent.horizontalCenter
        }
        TextField{
            id:name
            y:50
            width:parent.width-20
            anchors.horizontalCenter: parent.horizontalCenter
            placeholderText: "你的ID或名字-没有就想一个"
        }
        TextField{
            id:password
            y:75
            width:parent.width-20
            anchors.horizontalCenter: parent.horizontalCenter
            placeholderText: "密码-大于六位"
            echoMode: TextInput.Password
        }
        TextField{
            id:passwordrepeat
            y:100
            width:parent.width-20
            anchors.horizontalCenter: parent.horizontalCenter
            placeholderText: "重复密码,登录无需重复密码"
            echoMode: TextInput.Password
        }
        Image {
            id: imageF
            x:25
            y:passwordrepeat.y+passwordrepeat.height+20
            width: 100
            height: 100
            source: "image2.png"
        }
        Image {
            id: imageM
            x:175
            y:passwordrepeat.y+passwordrepeat.height+20
            width: 100
            height: 100
            source: "image.png"
        }
        ButtonGroup {
            id: radioGroup
        }
        RadioButton {
            id: radioButtonF
            checked:true
            x: 70
            y: imageF.y+imageF.height+10
        }
        RadioButton {
            id: radioButtonM
            x: 220
            y: imageF.y+imageF.height+10
        }
        Button {
            id:bottonLogin
            text: "登录"
            anchors {
                bottom: parent.bottom
                margins: 10
                right:parent.right
            }
            onClicked: {
            var nameOId=parseInt(name.text)
            if(!isNaN(nameOId)){
                remoteDB1.userLogin(nameOId,password.text)
                //welcomeWindow.destroy()
            }else{
                noticeText.text="没有输入正确的ID号！"
                notice.open()
                }
            }
        }
        Button {
            id:bottonRegister
            text: "注册"
            anchors {
                bottom: parent.bottom
                margins: 10
                right:bottonLogin.left
            }
            onClicked: {
                var p1=password.text
                var p2=passwordrepeat.text
                if(p1.length<6){
                    noticeText.text="密码长度小于六位！"
                    notice.open()
                }else if(p1!=p2){
                    noticeText.text="密码两次输入不一致！"
                    notice.open()
                }else{
                    if(radioButtonF.checked)root.photo=1
                    else root.photo=2
                    remoteDB1.userRegister(name.text,password.text,root.photo)
                    userName=name.text
                    //welcomeWindow.destroy()
                }
            }
        }
    }
    Dialog{
        id:notice//通知
        width:160
        height:100
        anchors.centerIn: parent
        title: qsTr("警告!")
        Text {
            id: noticeText
        }
        standardButtons: Dialog.Ok
    }
    Dialog{
        id:noticeOK//通知
        width:160
        height:100
        anchors.centerIn: parent
        title: qsTr("警告!")
        Text {
            id: noticeTextOK
        }
        standardButtons: Dialog.Ok
        onAccepted: {
            welcomeWindow.destroy()
        }
    }

}
